package com.rongzhe.house.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;

import com.rongzhe.house.R;
import com.rongzhe.house.entity.Dictionary;
import com.rongzhe.house.entity.voo.HouseDetail;
import com.rongzhe.house.presenter.LookForHousePresenter;
import com.rongzhe.house.utils.UIUtils;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;
import butterknife.OnTextChanged;

/**
 * Created by huang on 2018/1/3.
 */

public class LookForHouseActivity extends BaseActivity<LookForHousePresenter> {
    @BindView(R.id.text_house_name)
    TextView textHouseName;
    @BindView(R.id.edit_name)
    EditText editName;
    @BindView(R.id.edit_phone_number)
    EditText editPhoneNumber;
    @BindView(R.id.btn_choose_date)
    TextView btnChooseDate;
    @BindView(R.id.edit_leave_message)
    EditText editLeaveMessage;
    @BindView(R.id.btn_submit)
    Button btnSubmit;
    @BindView(R.id.image_house_photo)
    ImageView imageHousePhoto;
    @BindView(R.id.text_house_info)
    TextView textHouseInfo;
    @BindView(R.id.text_price)
    TextView textPrice;
    @BindView(R.id.text_man)
    TextView textMan;
    @BindView(R.id.text_fameil)
    TextView textFameil;
    @BindView(R.id.container_time_slot)
    Spinner containerTimeSlot;

    @Override
    protected LookForHousePresenter createPresenter() {
        return new LookForHousePresenter(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_look_for_house);
        ButterKnife.bind(this);
    }

    @OnCheckedChanged(R.id.switch_sex)
    public void onSexChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            mPresenter.setSex("2");
        } else {
            mPresenter.setSex("1");
        }
        textMan.setVisibility(isChecked ? View.GONE : View.VISIBLE);
        textFameil.setVisibility(isChecked ? View.VISIBLE : View.GONE);
        containerTimeSlot.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                mPresenter.setContactTimeOption(
                        ((MyAdapter)parent.getAdapter()).list.get(position).getCode());
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    private class MyAdapter extends BaseAdapter {
        public MyAdapter(List<Dictionary> list) {
            this.list = list;
        }
        List<Dictionary> list;
        @Override
        public int getCount() {
            return list == null ? 0 : list.size();
        }

        @Override
        public Object getItem(int position) {
            return list == null ? null : list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.list_item_text_spnner, parent, false);
            }
            ((TextView)convertView).setText(list.get(position).getName());
            return convertView;
        }
    }

    public void initTimeSlot(List<Dictionary> dictionaryList) {
        if (dictionaryList == null) {
            return;
        }
        MyAdapter myAdapter = new MyAdapter(dictionaryList);
       containerTimeSlot.setAdapter(myAdapter);
    }

    private CompoundButton.OnCheckedChangeListener mTimeSlotCheckedChange
            = new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (!isChecked) {
                return;
            }
            Dictionary dictionary = (Dictionary) buttonView.getTag();
            mPresenter.setContactTimeOption(dictionary.getCode());
        }
    };

    @OnClick(R.id.btn_choose_date)
    public void onChooseDateClick() {
        mPresenter.onChooseDateClick();
    }

    @OnTextChanged(R.id.edit_name)
    public void onNameChanged(CharSequence s, int start, int before, int count) {
        if (!TextUtils.isEmpty(s)) {
            mPresenter.setContact(s.toString());
        }
    }

    @OnTextChanged(R.id.edit_leave_message)
    public void onCommentsChanged(CharSequence s, int start, int before, int count) {
        if (!TextUtils.isEmpty(s)) {
            mPresenter.setMessage(s.toString());
        }
    }

    @OnTextChanged(R.id.edit_phone_number)
    public void onPhoneNumberChanged(CharSequence s, int start, int before, int count) {
        if (!TextUtils.isEmpty(s)) {
            mPresenter.setContactNumber(s.toString());
        }
    }

    @OnClick(R.id.btn_submit)
    public void onSubmitClick() {
        mPresenter.doSubmit();
    }

    @Override
    protected String getActionTitle() {
        return getString(R.string.booking_house);
    }

    public void setHouseDetailInfo(HouseDetail houseDetailInfo) {
        textHouseName.setText(houseDetailInfo.getName());
        if (houseDetailInfo.getHouseImageList() != null
                && houseDetailInfo.getHouseImageList().size() > 0)
            Picasso.with(this).load(houseDetailInfo.getHouseImageList()
                    .get(0).getUrl()).into(imageHousePhoto);
        textPrice.setText(houseDetailInfo.getRent().toString() + "元/月");
        textHouseInfo.setText(houseDetailInfo.getType() + "-" + (houseDetailInfo.getArea() + "㎡"));
    }

    public void setShowingDate(String time) {
        btnChooseDate.setText(time);
    }
}
